<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('index');
});
$path = Request::path();

$pathArr = explode('/',$path);
if (count($pathArr) <= 1) {
    $class = "IndexController";
} else {
    $class = ucfirst($pathArr[1])."Controller";
}

if (count($pathArr) <= 2) {
    $method = "indexAction";
} else {
    $method = $pathArr[2]."Action";
}
$namespace  = '';
$middleware = '';
switch ($pathArr[0]) {
    case 'command'  :{
        $namespace = 'Command';
        break;
    }
    case 'console'   :{
        $namespace = 'Console';
//        $middleware = 'webAuth';
        break;
    }
    default : {
        $class = ucfirst($pathArr[0])."Controller";
        if (count($pathArr) == 2) {
            $method =$pathArr[1]."Action";
        } else {
            $method = "indexAction";
        }
        break;
    }
}
$settings = ['prefix' => $pathArr[0] , 'namespace' => $namespace];
if ($middleware) {
    $settings['middleware'] = $middleware;
}

if ($namespace) {
    $fullClassName = 'App\\Http\\Controllers\\'.$namespace.'\\'.$class;
    if (!class_exists($fullClassName) || !method_exists($fullClassName,$method)) {
        abort(404);
    }
    Route::group($settings,function() use ($pathArr,$class,$method) {
        array_shift($pathArr);
        //echo implode('/',$pathArr);die();
//    echo $class.'@'.$method;die();
        Route::any(implode('/',$pathArr),$class.'@'.$method);
    });
}else{
    if($path != "/"){
        Route::any(implode('/',$pathArr),$class.'@'.$method);
    }

}

